/**
 * @(#) ReqCertiMediator.as
 *
 * @date		2009. 02. 17.
 * @version		1.0 ?
 * @author		Shin, Yunwoo
 * @description	제증명신청 - Mediator
 * 				제증명 발행을 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.certificate.view
{
	import com.skt.common_hr.certificate.CertificateFacade;
	import com.skt.common_hr.certificate.model.CertificateProxy;
	import com.skt.common_hr.certificate.model.CodesProxy;
	import com.skt.common_hr.certificate.model.WorkPlaceProxy;
	import com.skt.common_hr.certificate.model.vo.CertificateSaveVO;
	import com.skt.common_hr.certificate.model.vo.CertificateVO;
	import com.skt.common_hr.certificate.view.components.ReqCerti;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.wcl.utils.StringUtil;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;	

	public class ReqCertiMediator extends CommonMediator
	{
		public static const NAME:String = "ReqCertiMediator"; 
		
		private var selectedRec:CertificateVO;
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function ReqCertiMediator( viewComponent:Object, obj:CertificateVO )
        {
            super( NAME, viewComponent );
            selectedRec = obj;
            view.callLater(initView);
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		private function initView():void 
 		{
 			var codesProxy:CodesProxy = new CodesProxy();
 			facade.registerProxy(codesProxy);
 			codesProxy.load();
 			
 			var workPlaceProxy:WorkPlaceProxy = new WorkPlaceProxy();
 			facade.registerProxy(workPlaceProxy);
 			workPlaceProxy.load(Variables.myPernr); 
 			 
 			view.addEventListener(ReqCerti.OkInput, onOkInput);
 			view.addEventListener(ReqCerti.CancelInput, onCancelInput);
 		}
 		
 		
 		
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					CertificateFacade.LOAD_CODES_COMPLETE,
					CertificateFacade.LOAD_CODES_FAILED,
					CertificateFacade.LOAD_WORKPLACE_COMPLETE,
					CertificateFacade.LOAD_WORKPLACE_FAILED,
					CertificateFacade.INSERT_CERTIFICATE_COMPLETE,
					CertificateFacade.INSERT_CERTIFICATE_FAILED,
					CertificateFacade.UPDATE_CERTIFICATE_COMPLETE,
					CertificateFacade.UPDATE_CERTIFICATE_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case CertificateFacade.LOAD_CODES_COMPLETE:
					var proxy:CodesProxy = note.getBody() as CodesProxy;
					view.typeList = proxy.typeList;
					view.howList  = proxy.howList;
					view.whyList  = proxy.whyList;
					if(selectedRec!=null) {
						setFields(selectedRec);
					}
					break;
				case CertificateFacade.LOAD_CODES_FAILED:
					break;
				case CertificateFacade.LOAD_WORKPLACE_COMPLETE:
					var wpProxy:WorkPlaceProxy = note.getBody() as WorkPlaceProxy;
					if(Variables.company == 'NOS') {
						if(view.txtAcq.text  == '') {
							view.txtAcq.text = wpProxy.place;	
						}
							
					}
					else {
						view.workPlace = wpProxy.place;
					}
					
					break;
				case CertificateFacade.INSERT_CERTIFICATE_COMPLETE:
					CommonAlert.showMessage(commonFacade.bundle, "ALERT_RESULT_01");
					facade.sendNotification(CertificateFacade.OK_REQUEST_INPUT, this);
					break;
				case CertificateFacade.UPDATE_CERTIFICATE_COMPLETE:
					CommonAlert.showMessage(commonFacade.bundle, "ALERT_RESULT_02"); 
					facade.sendNotification(CertificateFacade.OK_REQUEST_INPUT, this);
					break;
				case CertificateFacade.INSERT_CERTIFICATE_FAILED:					
					Alert.show(CertificateProxy(note.getBody()).retText);
					break;
				case CertificateFacade.UPDATE_CERTIFICATE_FAILED:					
					Alert.show(CertificateProxy(note.getBody()).retText); 
					break;					
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;			
			}
		}
		
		/**
		 * Main Module에서 QueryClose 메세지가 왔을 때의 처리
		 *     - 사용자에게 확인하고 View에 종료처리 -> MainModule에 'CLOSE_MODULE' 전송
		 * @param
		 * @return void
		 */
		
		 
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
 		/**
		 * 모듈 Close 확인 창을 사용자가 확인후 처리
		 *     - Close OK     : View에 종료처리 -> MainModule에 'CLOSE_MODULE' 전송
		 *	   - Close Cancel : 처리 없음 
		 * @param
		 * @return void
		 */

 		private function onOkInput(event:Event):void
 		{
 			var dtype:String = view.typeList.getItemAt(view.cbType.selectedIndex).CODE;
 			var copy:String = view.nsCopy.value.toString();
 			var whyneed:String = view.whyList.getItemAt(view.cbWhy.selectedIndex).CODE;
 			var howsend:String = view.howList.getItemAt(view.cbHow.selectedIndex).CODE;
 			var acq:String = view.txtAcq.text;
 			
 			if(StringUtil.NVL(dtype) == ""){
 				CommonAlert.showMessage(commonFacade.bundle, "ALERT_INVALID_INPUT_01");
 				return;
 			}else if(StringUtil.NVL(whyneed) == ""){
 				CommonAlert.showMessage(commonFacade.bundle, "ALERT_INVALID_INPUT_02");
 				return;
 			}else if(StringUtil.NVL(howsend) == ""){
 				CommonAlert.showMessage(commonFacade.bundle, "ALERT_INVALID_INPUT_03");
 				return;
 			}
 			
 			if( (dtype == "04" || dtype == "05" || dtype == "06") && Variables.company == 'NOS') {
 				if(view.txtRyear.text.length < 4) {
 					view.txtRyear.setFocus();
 					Alert.show("발급년도를 확인해주세요.");
 					return;
 				}
 			}
 			
 			var vo:CertificateSaveVO = new CertificateSaveVO;
 			vo.DTYPE 	= dtype;
 			vo.COPY  	= copy;
 			vo.WHYNEED	= whyneed;
 			vo.HOWSEND  	= howsend;
 			vo.ACQ_TXT  	= acq;
 			if( (dtype == "04" || dtype == "05" || dtype == "06") && Variables.company == 'NOS') {
 				vo.RYEAR  		= view.txtRyear.text;	
 			}
 			
 			var proxy:CertificateProxy = facade.retrieveProxy(CertificateProxy.NAME) as CertificateProxy;

 			if(selectedRec==null) {		// 신규 등록인경우
	 			proxy.insert(Variables.myPernr, vo);
	 		} 
			else {						// 수정인 경우
				vo.SEQNR = selectedRec.SEQNR;
				proxy.update(Variables.myPernr, vo);
			}
 			//facade.sendNotification(CertificateFacade.OK_REQUEST_INPUT, this);
 		}
 		
 		private function onCancelInput(event:Event):void
 		{
 			view.removeEventListener(ReqCerti.CancelInput, onCancelInput);
 			view.removeEventListener(ReqCerti.OkInput, onOkInput);
 			facade.sendNotification(CertificateFacade.CANCEL_REQUEST_INPUT, this);
 		}
 		



		private function setFields(vo:CertificateVO):void
		{
			view.cbType.selectedIndex = findCodeIndex(vo.DTYPE, view.typeList);
			view.nsCopy.value = vo.COPY;
			view.cbHow.selectedIndex = findCodeIndex(vo.HOWSEND, view.howList);
			view.cbWhy.selectedIndex = findCodeIndex(vo.WHYNEED, view.whyList);
			view.txtAcq.text = vo.ACQ_TXT;
			
			if(Variables.company == 'NOS') {
				if(vo.DTYPE == '04' || vo.DTYPE == '05' ||  vo.DTYPE == '06') {
					view.fiRyear.visible = true;
					view.fiRyear.includeInLayout = true;
								
					view.txtRyear.text = 	vo.RYEAR;
				}	
			}
			
		}
		
		
		private function findCodeIndex(key:String, srcArr:ArrayCollection):int 
		{
			for(var i:int=0; i<srcArr.length; i++) {
				if(srcArr[i].CODE == key) {
					return i;
				}
			}
			return -1;
		}
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():ReqCerti
        {
            return viewComponent as ReqCerti;
        }
		
		
	}
}